package com.framework.web.controller.system;

import com.framework.common.annotation.QueryTarget;
import com.framework.common.model.validation.ValidationGroup;
import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.system.SystemUtil;
import com.framework.model.system.vo.SystemMenuVo;
import com.framework.service.system.SystemMenuService;
import com.framework.web.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.web.controller.system
 * @description 菜单请求控制类
 * @datetime 2019-01-09 11:11:11
 */
@Validated
@Controller
@RequestMapping(value = "/system/menu")
public class SystemMenuController extends BaseController {
    private String path = "lowerright/system/menu/";
    @Autowired
    private SystemMenuService systemMenuServiceImpl;

    /**
     * @return org.springframework.web.servlet.ModelAndView
     * @title 分页页面跳转
     * @description 分页页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/page/list")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_MENU_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT')")
    public ModelAndView pageList() {
        return new ModelAndView(path + "menuList");
    }

    /**
     * @param id 1 编号
     * @return org.springframework.web.servlet.ModelAndView
     * @title 新增页面跳转
     * @description 新增页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/add")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:add')")
    public ModelAndView getAdd(@NotNull(message = "请选择上级菜单")
                               @Min(value = NumeralUtil.POSITIVE_ZERO, message = "上级菜单不存在")
                               @Max(value = Long.MAX_VALUE, message = "上级菜单不存在")
                                       Long id) {
        ModelAndView mv = new ModelAndView(path + "menuAdd");
        mv.addObject("p", systemMenuServiceImpl.selectByPrimaryKey(id));
        return mv;
    }

    /**
     * @param id 1 编号
     * @return org.springframework.web.servlet.ModelAndView
     * @title 编辑页面跳转
     * @description 编辑页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/edit")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:edit')")
    public ModelAndView getEdit(@NotNull(message = "请选择菜单")
                                @Min(value = NumeralUtil.POSITIVE_ONE, message = "菜单不存在")
                                @Max(value = Long.MAX_VALUE, message = "菜单不存在")
                                        Long id) {
        ModelAndView mv = new ModelAndView(path + "menuEdit");
        mv.addObject("p", systemMenuServiceImpl.getByIdParam(id));
        return mv;
    }

    /**
     * @param id 1 编号
     * @return org.springframework.web.servlet.ModelAndView
     * @title 查看页面跳转
     * @description 查看页面跳转
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @GetMapping("/get/view")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:view')")
    public ModelAndView getView(@NotNull(message = "请选择菜单")
                                @Min(value = NumeralUtil.POSITIVE_ONE, message = "菜单不存在")
                                @Max(value = Long.MAX_VALUE, message = "菜单不存在")
                                        Long id) {
        ModelAndView mv = new ModelAndView(path + "menuView");
        mv.addObject("p", systemMenuServiceImpl.getByIdParam(id));
        return mv;
    }

    /**
     * @param row 1 菜单实体类对象
     * @return com.framework.common.model.response.R
     * @title 菜单分页查询
     * @description 菜单分页查询
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    // method = RequestMethod.POST,
    @GetMapping(value = "/find/page/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:find_page_list')")
    @QueryTarget
    public R<?> findPageList(@Validated(value = {ValidationGroup.formPageQuery.class}) SystemMenuVo row) {
        return systemMenuServiceImpl.findParamPageList(row);
    }

    /**
     * @param id      1 编号
     * @param pId     2 上级编号
     * @param keyword 3 模糊搜索
     * @return com.framework.common.model.response.R
     * @title 菜单树形查询
     * @description 菜单树形查询
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @RequestMapping(value = "/find/menu/tree/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_MENU_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT')")
    public R<?> findMenuTreeList(@Min(value = NumeralUtil.POSITIVE_ZERO, message = "菜单不存在")
                                           @Max(value = Long.MAX_VALUE, message = "菜单不存在")
                                                   Long id,
                                           @Min(value = NumeralUtil.POSITIVE_ZERO, message = "上级菜单不存在")
                                           @Max(value = Long.MAX_VALUE, message = "上级菜单不存在")
                                                   Long pId,
                                           @Size(message = "关键字最多64个字符", max = NumeralUtil.POSITIVE_SIXTY_FOUR)
                                                   String keyword) {
        return systemMenuServiceImpl.findMenuTreeList(id, pId, keyword);
    }

    /**
     * @param row 1 菜单实体类对象
     * @return com.framework.common.model.response.R
     * @title 新增
     * @description 新增
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:add')")
    public R<?> save(@Validated(value = {ValidationGroup.formAdd.class}) SystemMenuVo row) {
        return systemMenuServiceImpl.save(row);
    }

    /**
     * @param row 1 菜单实体类对象
     * @return com.framework.common.model.response.R
     * @title 修改
     * @description 修改
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:edit')")
    public R<?> edit(@Validated(value = {ValidationGroup.formEdit.class}) SystemMenuVo row) {
        return systemMenuServiceImpl.edit(row);
    }

//    /**
//     * @param idList 1 菜单编号集合
//     * @return com.framework.common.model.response.R
//     * @title 批量删除
//     * @description 批量删除
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @PostMapping(value = "/batch/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ResponseBody
//    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:batchDel')")
//    public R<?> batchDel(@NotEmpty(message = "请选择菜单")
//                                   @Size(min = NumeralUtil.POSITIVE_ONE, message = "菜单不存在")
//                                   @RequestParam(value = "idList[]") List<Long> idList) {
//        return systemMenuServiceImpl.batchDeleteList(idList);
//    }

    /**
     * @param id 1 菜单编号
     * @return com.framework.common.model.response.R
     * @title 删除
     * @description 删除
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     */
    @DeleteMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:del')")
    public R<?> del(@NotNull(message = "请选择菜单")
                              @Min(value = NumeralUtil.POSITIVE_ONE, message = "菜单不存在")
                              @Max(value = Long.MAX_VALUE, message = "菜单不存在")
                                      Long id) {
        return systemMenuServiceImpl.delId(id);
    }

//    /**
//     * @param id       1 菜单编号
//     * @param menuCode 2 菜单代码
//     * @return com.framework.common.model.response.R
//     * @title 验证是否重复菜单code
//     * @description 验证是否重复菜单code
//     * @author 龘鵺
//     * @datetime 2019-01-09 11:11:11
//     */
//    @GetMapping("/isExist")
//    @ResponseBody
//    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:isExist')")
//    public R<?> isExist(Long id, String menuCode) {
//        return systemMenuServiceImpl.isExist(id, menuCode);
//    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 启用|禁用
     * @description 启用|禁用
     * @author 龘鵺
     * @datetime 2019-01-09 11:11:11
     * @version 1.0
     */
    @ResponseBody
    @GetMapping("/enable")
    @PreAuthorize("hasPermission('" + SystemUtil.SYSTEM_BUTTON_NAME + "','SYSTEM_MENU_LIST_MANAGEMENT:enable')")
    public R<?> enable(@NotNull(message = "请选择菜单")
                                 @Min(value = NumeralUtil.POSITIVE_ONE, message = "菜单不存在")
                                 @Max(value = Long.MAX_VALUE, message = "菜单不存在")
                                         Long id) throws Exception {
        return systemMenuServiceImpl.enable(id);
    }

}
